package com.zhengsr.wanandroid.mvp.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.zhengsr.wanandroid.Constant;
import com.zhengsr.wanandroid.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import qiu.niorgai.StatusBarCompat;

/**
 * Created by zhengshaorui
 * time: 2018/8/19
 */

public abstract class BaseFragment<T extends BasePresent> extends DelegateFragment
    implements IBaseView{
    private static final String TAG = "BaseFragment";
    private static final int NORMAL_VIEW = 1;
    private static final int LOADING_VIEW = 2;
    private static final int ERROR_VIEW = 3;

    private T mPresent;
    private View mLoadingView;
    private View mNormalView;
    private View mErrorView;
    private Unbinder mUnbinder;
    private ArticleLocalReceiver mLocalReceiver;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarCompat.setStatusBarColor(_mActivity,
                _mActivity.getResources().getColor(R.color.maincolor));
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        FrameLayout view = (FrameLayout) LayoutInflater.from(getContext()).inflate(R.layout.frame_base,container,false);
        mNormalView = LayoutInflater.from(getContext()).inflate(getLayoutId(),view,false);
        mUnbinder = ButterKnife.bind(this,mNormalView);
        mPresent = getPresent();
        if (mPresent != null){
            mPresent.attachView(this);
        }
        return view;
    }


    @Override
    public void onDestroyView() {
        if (mPresent != null){
            mPresent.detachView();
            mPresent = null;
        }
        if (mUnbinder != null){
            mUnbinder.unbind();
            mUnbinder = null;
        }

        if (mLocalReceiver != null) {
            LocalBroadcastManager.getInstance(_mActivity).unregisterReceiver(mLocalReceiver);
        }
        super.onDestroyView();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

        if (getView() == null){
            return;
        }

        ViewGroup parent = (ViewGroup) getView();
        mLoadingView = LayoutInflater.from(_mActivity).inflate(R.layout.loadingview_layout,parent,false);
        mErrorView = LayoutInflater.from(_mActivity).inflate(R.layout.load_error_layout,parent,false);

        mErrorView.findViewById(R.id.error_reload_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reload();
            }
        });

        parent.addView(mLoadingView);
        parent.addView(mErrorView);
        parent.addView(mNormalView);
        initView(mNormalView);
        if (showNormalView()){
            showCurrentView(NORMAL_VIEW);
        }else {
            showCurrentView(LOADING_VIEW);
        }

        mLocalReceiver = new ArticleLocalReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constant.ACTION_SEND_ARTICLE_ID);
        LocalBroadcastManager.getInstance(_mActivity).registerReceiver(mLocalReceiver,intentFilter);

    }


    @Override
    public void useNightMode(boolean isNightMode) {

    }

    @Override
    public void showLoading() {
        showCurrentView(LOADING_VIEW);
    }


    @Override
    public void loadSuccess() {
        showCurrentView(NORMAL_VIEW);
    }

    @Override
    public void showError() {
        showCurrentView(ERROR_VIEW);
    }


    public abstract int getLayoutId();
    public abstract T getPresent();
    public abstract void initView(View view);


    private void showCurrentView(int type){
        if (mErrorView == null || mNormalView == null || mLoadingView == null){
            return;
        }

        mNormalView.setVisibility(View.GONE);
        mLoadingView.setVisibility(View.GONE);
        mErrorView.setVisibility(View.GONE);

        if (type == NORMAL_VIEW){
            mNormalView.setVisibility(View.VISIBLE);
        }else if (type == LOADING_VIEW){
            mLoadingView.setVisibility(View.VISIBLE);
        }else if (type == ERROR_VIEW){
            mErrorView.setVisibility(View.VISIBLE);
        }
    }

    protected boolean showNormalView(){
        return true;
    }

    @Override
    public void showErrorMsg(String msg) {
        showError();
    }

    public void reload(){

    }



    class ArticleLocalReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            getLocalReceiver(intent);
        }
    }

    protected void getLocalReceiver(Intent intent){

    }






}
